package com.dtsheep.elasticsearch.es.service;

import com.dtsheep.elasticsearch.es.document.UserDocument;
import com.dtsheep.elasticsearch.es.repository.UserDocumentRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * UserService
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/5/24
 */
@Service
public class UserDocumentService {

    @Resource
    private UserDocumentRepository userDocumentRepository;

    /**
     * 根据id查文档
     */
    public UserDocument queryById(Long id){
        Optional<UserDocument> res = userDocumentRepository.findById(id);
        return res.get() != null ? res.get() : null;
    }

    /**
     * 查询所有文档
     */
    public List<UserDocument> queryAll(){
        Iterable<UserDocument> userDocuments = userDocumentRepository.findAll();
        List<UserDocument> userList = StreamSupport.stream(userDocuments.spliterator(), false)
                .collect(Collectors.toList());
        return userList;
    }

    /**
     * 添加文档
     */
    public Integer insert(UserDocument userDocument){
        UserDocument document = userDocumentRepository.save(userDocument);
        System.out.println(document);
        return 1;
    }


    /**
     * 批量添加文档
     *
     * 没有给定id时候会自动随机生成id
     * eg.6nCwzI8B9psJNreVHWXM
     * 添加已经存在的id时会进行文档覆盖
     *
     */
    public Integer insertBatch(List<UserDocument> userDocument) {
        Iterable<UserDocument> userDocumentIterable = userDocumentRepository.saveAll(userDocument);
        List<UserDocument> userDocuments = StreamSupport.stream(userDocumentIterable.spliterator(), false)
                .collect(Collectors.toList());
        System.out.println(userDocuments);
        return userDocuments.size();
    }

    /**
     * 根据文档对象删除文档
     *
     */
    public Boolean deleteByEntity(UserDocument userDocument){
        userDocumentRepository.delete(userDocument);
        return Boolean.TRUE;
    }

    /**
     * 根据文档id删除文档
     *
     * 文档id没有的就不会删除
     *
     */
    public Boolean deleteById(Long id){
        userDocumentRepository.deleteById(id);
        return Boolean.TRUE;
    }

    /**
     *  模糊搜索
     *
     *  文档对象的id字段必传！！！，pageable不能为空！！！！！
     *  integer类型的不能模糊查询！！！
     *
     */
    public Page<UserDocument> search(String sex){
        String[] fields={"sex"};
        UserDocument userDocument = new UserDocument();
        userDocument.setSex(sex);
        Page<UserDocument> userDocuments = userDocumentRepository.searchSimilar(userDocument, fields, PageRequest.ofSize(10));
        return userDocuments;
    }


    /**
     * page进行分页
     */
    public Page<UserDocument> findAllProductsSortedAndPaged(int page, int size, String sortBy, Sort.Direction direction) {
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by(direction, sortBy));
        return userDocumentRepository.findAll(pageRequest);
    }

    /**
     * 条件搜索
     */
    public List<UserDocument> searchTest(){
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.termsQuery("age", "21"));
//        Iterable<UserDocument> search = userDocumentRepository.

//        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timestamp")
//                .gte(qryDTO.getStatisticsStartTime())  // 大于等于起始时间
//                .lte(qryDTO.getStatisticsEndTime());   // 小于等于结束时间
//        boolQueryBuilder.filter(rangeQuery);
//
        Iterable<UserDocument> search = userDocumentRepository.search(boolQueryBuilder);
        List<UserDocument> userDocuments = StreamSupport.stream(search.spliterator(), false).collect(Collectors.toList());
        return userDocuments;
    }

}
